#!/usr/bin/env python
#   vim: fileencoding=utf-8

# This file is part of open-hea.
#
# open-hea is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation, either version 3 of the License, or (at your
# option) any later version.
#
# open-hea is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with open-hea.  If not, see <http://www.gnu.org/licenses/>.

from sqlalchemy import Column
from sqlalchemy import Date
from sqlalchemy import Float
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy import UniqueConstraint
from sqlalchemy.dialects import mysql
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql.expression import text
from sqlalchemy.orm import relationship

from geoalchemy import GeometryColumn
from geoalchemy import GeometryDDL
from geoalchemy import Polygon

Base = declarative_base()
metadata = Base.metadata


class Project(Base):
    __tablename__ = "projects"

    __table_args__ = {
        "mysql_engine": "InnoDB",
        "mysql_charset": "utf8"
    }

    id = Column("id", Integer(), nullable=False, primary_key=True)
    name = Column("project", String(length=255),
                  nullable=True, server_default=text("NULL"))
    createdBy = Column("createdby", String(length=255),
                       nullable=True, server_default=text("NULL"))
    dateCreated = Column("datecreated", Date(),
                         nullable=True, server_default=text("NULL"))
    currency = Column("currency", String(length=255),
                      nullable=True, server_default=text("NULL"))
    agroYearBegins = Column("agroyearbeggins", String(length=155),
                            nullable=True, server_default=text("NULL"))
    livelihoodzones = relationship("ProjectLivelihoodZone")


class LivelihoodZone(Base):
    __tablename__ = "livelihoodzone"

    __table_args__ = (UniqueConstraint("livelihoodzone",
                      name="livelihoodzone_UNIQUE"),
                      {"mysql_engine": "InnoDB",
                      "mysql_charset": "utf8"}
                      )

    id = Column("id", Integer(), nullable=False, primary_key=True)
    name = Column("livelihoodzone", String(length=255),
                  nullable=True, server_default=text("NULL"),
                  unique=True, index=True)
    createdBy = Column("createdby", String(length=155),
                       nullable=True, server_default=text("NULL"))
    dateCreated = Column("datecreated", Date(),
                         nullable=True, server_default=text("NULL"))
    population = Column("population",
                        Integer().with_variant(mysql.DOUBLE(), "mysql"),
                        nullable=True, server_default=text("NULL"))
    coordinates = GeometryColumn("coordinates", Polygon(),
                                 nullable=True, server_default=text("NULL"))
    consumptionYearBegins = Column("consumptionyearbegins", String(length=45),
                                   nullable=True, server_default=text("NULL"))


class ProjectLivelihoodZone(Base):
    __tablename__ = "project_livelihoodzones"

    __table_args__ = {
        "mysql_engine": "InnoDB",
        "mysql_charset": "utf8"
    }

    id = Column("id", Integer(), nullable=False, primary_key=True)
    projectId = Column("projectid", Integer, ForeignKey("projects.id"))
    livelihoodZoneId = Column("livelihoodzoneid", Integer,
                              ForeignKey("livelihoodzone.id"))
    zone = relationship("LivelihoodZone")

GeometryDDL(LivelihoodZone.__table__)
declaration_ordering = [Project, LivelihoodZone, ProjectLivelihoodZone]
